package com.smile.menu;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.light.roller.BigMenu;
import com.light.roller.ResourceManager;
import com.light.roller.Roller_1;
import com.light.roller.Roller_2;
import com.light.roller.Roller_3;
import com.light.roller.Roller_S10;
import com.light.roller.Roller_S11;
import com.light.roller.Roller_S12;
import com.light.roller.Roller_S13;
import com.light.roller.Roller_S14;
import com.light.roller.Roller_S15;
import com.light.roller.Roller_S16;
import com.light.roller.Roller_S17;
import com.light.roller.Roller_S18;
import com.light.roller.Roller_S19;
import com.light.roller.Roller_S20;
import com.light.roller.Roller_S21;
import com.light.roller.Roller_S22;
import com.light.roller.Roller_S23;
import com.light.roller.Roller_S24;
import com.light.roller.Roller_S25;
import com.light.roller.Roller_S26;
import com.light.roller.Roller_S27;
import com.light.roller.Roller_S3;
import com.light.roller.Roller_S4;
import com.light.roller.Roller_S5;
import com.light.roller.Roller_S6;
import com.light.roller.Roller_S7;
import com.light.roller.Roller_S8;
import com.light.roller.Roller_S9;
import com.light.roller.Roller_T1;
import com.light.roller.Roller_T2;

public class MenuSpotManager {

	protected Vector2[] spotPositionList;	
	protected MenuButton[] menuButtons;
		
	public static Vector2 destinationSpot;
	private static Vector2 currentSpot;
	
	private final static float walkDistance = 3.0f*1.414f; 
	private int steps;
	
	public BigMenu m_menu;
	
	public static int currentTargetNum=0;
	private static int finalTargetNum=0;
	
	private int unLockedMenu;
	private boolean isGoForward;
	
	protected Class<?>[] stages;
	
	
	protected Sprite[] introPic;
	
	protected String[] stageString;
	
	public MenuSpotManager(BigMenu menu)
	{
		m_menu = menu;
	}
	
	
	public void onLoadScene(Scene scene ,PhysicsWorld physicsworld)
	{
		for(int i=0;i<menuButtons.length;i++)
		{
			menuButtons[i].onLoadScene(scene, physicsworld);
		}
		
	}
	
	
	public void initialize(int unLockedNum)
	{
		this.stages = this.m_menu.getStages();
		this.menuButtons = this.m_menu.getMenuButtons();
		this.spotPositionList = this.m_menu.getSpotPositionList();
		this.stageString = this.m_menu.getStageString();
		
		for(int i=0;i<menuButtons.length&&i<unLockedNum;i++)
		{
			menuButtons[i].setLocked(false);
		}
	}
	
	public void initialPlayerPosition(AnimatedSprite m_sprite)
	{
		m_sprite.setPosition(spotPositionList[finalTargetNum].x-m_sprite.getWidth()/2, spotPositionList[finalTargetNum].y-m_sprite.getHeight()/2);
	}
	
	
	public Vector2 getNextSpot(Vector2 currentSpotPosition)
	{
		currentSpot = currentSpotPosition;
		
		int destinationSpotNum = findSpotNum(destinationSpot);
		
		if(currentTargetNum>destinationSpotNum)
		{
			currentTargetNum = currentTargetNum-1;
			return spotPositionList[currentTargetNum];
		}else if(currentTargetNum<destinationSpotNum)
		{
			currentTargetNum = currentTargetNum+1;
			return spotPositionList[currentTargetNum];
		}else
		{
			return destinationSpot;
		}
	}
	
	public void setDestination(Vector2 destinationSpotPosition)
	{
		destinationSpot = destinationSpotPosition;
	}
	
	public void setDestination(MenuButton destinationButton)
	{
		Vector2 buttonPosition = new Vector2(destinationButton.centerX,destinationButton.centerY);
		int spotNum = findSpotNum(buttonPosition);
		
		if(spotNum>finalTargetNum)
		{
			this.isGoForward = true;
		}else
		{
			this.isGoForward = false;
		}
		destinationSpot = spotPositionList[spotNum];
		finalTargetNum = spotNum;
	}
	
	public void setDestination(int spotNum)
	{
		destinationSpot = spotPositionList[spotNum];
		finalTargetNum = spotNum;
	}
	
	public boolean isAbout(float position, float targetPosition)
	{
		if(position<targetPosition+2f && position>targetPosition-2f)
		{
			return true;
		}else
		{
			return false;
		}
	}
	
	public int findSpotNum(Vector2 currentSpotPostion)
	{
		int spotNum=0;
		for(spotNum=0;spotNum<spotPositionList.length;spotNum++)
		{
			if(isAbout(spotPositionList[spotNum].x,currentSpotPostion.x)
				&&isAbout(spotPositionList[spotNum].y,currentSpotPostion.y))
			{
				return spotNum;
			}
		}
		
		return -1;
	}
	
	public boolean isOntarget(Vector2 currentSpotPosition)
	{
		if(isAbout(destinationSpot.x,currentSpotPosition.x)
				&& isAbout(destinationSpot.y ,currentSpotPosition.y))
		{
			m_menu.switchToIdleAnimation();
			currentTargetNum = finalTargetNum;
			return true;
		}
		
		m_menu.printText.setText("touch"+spotPositionList[currentTargetNum].x+" "+spotPositionList[currentTargetNum].y+"  "+currentTargetNum);
		
		
		
		
		return false;
	}
	
	public boolean isOnStageSpot(Vector2 currentSpotPosition)
	{
		if(isAbout(spotPositionList[currentTargetNum].x,currentSpotPosition.x)
				&& isAbout(spotPositionList[currentTargetNum].y ,currentSpotPosition.y))
		{
			Vector2 nextSpot = getNextSpot(currentSpotPosition);
			m_menu.setTouchPoint(nextSpot.x, nextSpot.y);
			setSpeed(currentTargetNum);
			return true;
		}
		
		return false;
	}
	
	public void setSpeed(int spotNum)
	{
		float distance;
		float distanceX;
		float distanceY;
		float speedX;
		float speedY;
		distanceX = m_menu.m_sprite.getX()+(m_menu.m_sprite.getWidth()/2)-spotPositionList[spotNum].x;
		distanceY = m_menu.m_sprite.getY()+(m_menu.m_sprite.getHeight()/2)-spotPositionList[spotNum].y;
		distance = (float) Math.sqrt(Math.pow(distanceX, 2)+ Math.pow(distanceY, 2));
		steps = (int) Math.ceil(distance/walkDistance);
		speedX=Math.abs(distanceX/steps);
		speedY=Math.abs(distanceY/steps);
		m_menu.setWalkSpeed(speedX, speedY);
		m_menu.switchToWalkAnimation();
		m_menu.setTouchPoint(spotPositionList[currentTargetNum].x,spotPositionList[currentTargetNum].y);
		
		if(distanceX<0)
		{
			m_menu.m_sprite.getTextureRegion().setFlippedHorizontal(true);
		}else if(distanceX>0)
		{
			m_menu.m_sprite.getTextureRegion().setFlippedHorizontal(false);
		}	
	}

	public void onLoadResources()
	{
		for(int i=0;i<menuButtons.length;i++)
		{
			menuButtons[i].onLoadResources();
		}
	}
	
	public void setUnLockedMenu(int unlocked)
	{
		unLockedMenu = (int)Math.floor(unlocked/15);
	}
	
	public Class<?> getTargetClass()
	{
		return this.stages[this.finalTargetNum];
	}
	
	public String getTargetString()
	{
		return stageString[this.finalTargetNum];
	}
	
	public Class<?> getCurrentStage()
	{
		if(this.isGoForward)
		{
			return stages[this.currentTargetNum-1];

		}else
		{
			return stages[this.currentTargetNum+1];

		}
	}
	
	public int searchForStageNum(Class<?> target)
	{
		for(int i = 0;i<stages.length;i++)if(target==stages[i])return i;
		return getTargetStageNum();
		
	}
	

	public String getCurrentString()
	{
		if(this.isGoForward)
		{
			return stageString[this.currentTargetNum-1];

		}else
		{
			return stageString[this.currentTargetNum+1];

		}
	}
	
	public int[] getFinalPic()
	{
		int[] returnSpriteNum;
		
		if(this.finalTargetNum == 0)
		{
			returnSpriteNum = new int[]
			{
				this.finalTargetNum+2,
				this.finalTargetNum,
				this.finalTargetNum+1
			};
		}else if(this.finalTargetNum == (this.stages.length-1))
		{
			returnSpriteNum = new int[]
			              			{
			              				this.finalTargetNum-1,
			              				this.finalTargetNum,
			              				this.finalTargetNum-2
			              			};
		}else
		{
			returnSpriteNum = new int[]
			              			{
			              				this.finalTargetNum-1,
			              				this.finalTargetNum,
			              				this.finalTargetNum+1
			              			};
		}
		return returnSpriteNum;
	}
	
	public int[] getCurrentPic()
	{
		int[] returnSpriteNum;
		
		if(this.isGoForward)
		{
			if(this.currentTargetNum>=2)
			{
				returnSpriteNum = new int[]
			               {
							  this.currentTargetNum-2,
							  this.currentTargetNum-1,
							  this.currentTargetNum
			               };
			}else
			{
				returnSpriteNum = new int[]
				           {
							  this.currentTargetNum+1,
							  this.currentTargetNum-1,
							  this.currentTargetNum
				           };
			}
			

		}else
		{
			if(this.currentTargetNum<this.stages.length-2)
			{
				returnSpriteNum = new int[]
			               {
							  this.currentTargetNum,
							  this.currentTargetNum+1,
							  this.currentTargetNum+2
			               };
			}else
			{
				returnSpriteNum = new int[]
				           {
							  this.currentTargetNum,
							  this.currentTargetNum+1,
							  this.currentTargetNum-1
				           };
			}
		}
		return returnSpriteNum;
	}
	
	public float getPicSpeed()
	{
		if(this.isGoForward)
		{
			return this.steps;
		}else
		{
			return -this.steps;
		}
	}
	
	public int getStageNum()
	{
		return this.stages.length;
	}
	
	public Vector2 getStagePosition(int stageNum)
	{
		return this.spotPositionList[stageNum];
	}
	
	public int getTargetStageNum()
	{
		return this.finalTargetNum;
	}
}
